<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <form action="" @submit.prevent="insert">
            <input type="text" v-model="student.name">
            <input type="text" v-model="student.age">
            <input type="text" v-model="student.girlfriend.name">
            <button>添加</button>
        </form>
        <ui>
            <li v-for="(item, index) in list" :key="index">
                姓名：{{item.name}}
                年龄：{{item.age}}
                女朋友：{{item.girlfriend.name}}
                <input type="radio">
            </li>
        </ui>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script>
        new Vue({
            el:'#app',
            data:{
                student:{
                    name:'',
                    age:'',
                    girlfriend:{
                        name:''
                    }
                },
                list:[],
            },
            methods: {
                insert(){
                    this.list.push(
                        this.copy(this.student)
                        // this.deepCopy(this.student)
                        // this.JSONDeepCopy(this.student)
                    )
                },
                // 浅拷贝
                copy(obj){
                    let newObj={}
                    for( i in obj){
                        newObj[i] = obj[i]
                    }
                    return newObj
                },
                // 深拷贝
                deepCopy(obj){
                    let newObj={}
                    for( i in obj){
                        if(obj[i] instanceof Object){
                            newObj[i] = this.copy(obj[i])
                        }else{
                            newObj[i] = obj[i]
                        }
                    }
                    return newObj
                },
                // 通过JSON实现深拷贝
                JSONDeepCopy(obj){
                    let str = JSON.stringify(obj)
                    let newObj = JSON.parse(str)
                    return newObj
                }
            },
        })
    </script>
</body>
</html>